1 using System;
2 using
UnityEngine;
3 using
Random = UnityEngine.Random;
4
5     
[RequireComponent(typeof(Motor))]
6     
public class CarAudioToy : MonoBehaviour
7     {
8
9         
public AudioClip AccelClip;
10  
11         
public float pitchMultiplier = 1f;
12         
public float lowPitchMin = 1f;
13         
public float lowPitchMax = 6f;
14         
public float highPitchMultiplier = 0.25f;
15         
public float maxRolloffDistance = 500;
16         
public float dopplerLevel = 1;
17         
public bool useDoppler = true;
18
19         
private AudioSource m_HighAccel;
20
21         
private bool m_StartedSound;
22         
private Motor m_CarController;
23
24
25         
private void StartSound()
26         {
27             m_CarController = GetComponent<Motor>();
28             m_HighAccel = SetUpEngineAudioSource(AccelClip);
29             m_StartedSound =
true;
30         }
31
32
33         
private void StopSound()
34         {
35             
foreach (var source in GetComponents<AudioSource>())
36             {
37                 Destroy(source);
38             }
39
40             m_StartedSound =
false;
41         }
42
43         
private void Update()
44         {
45             
// get the distance to main camera
46             
float camDist = (Camera.main.transform.position - transform.position).sqrMagnitude;
47
48             
// stop sound if the object is beyond the maximum roll off distance
49             
if (m_StartedSound && camDist > maxRolloffDistance * maxRolloffDistance)
50             {
51                 StopSound();
52             }
53
54             
// start the sound if not playing and it is nearer than the maximum distance
55             
if (!m_StartedSound && camDist < maxRolloffDistance * maxRolloffDistance)
56             {
57                 StartSound();
58             }
59
60             
if (m_StartedSound)
61             {
62                 
// The pitch is interpolated between the min and max values, according to the car's revs.
63                 
float pitch = ULerp(lowPitchMin, lowPitchMax, m_CarController.Revs);
64
65                 
// clamp to minimum pitch (note, not clamped to max for high revs while burning out)
66                 pitch = Mathf.Min(lowPitchMax, pitch);
67
68                     m_HighAccel.pitch = pitch * pitchMultiplier * highPitchMultiplier;
69                     m_HighAccel.dopplerLevel = useDoppler ? dopplerLevel :
0;
70                     m_HighAccel.volume =
1;
71
72             }
73         }
74
75         
private AudioSource SetUpEngineAudioSource(AudioClip clip)
76         {
77             
// create the new audio source component on the game object and set up its properties
78             AudioSource source = gameObject.AddComponent<AudioSource>();
79             source.clip = clip;
80             source.volume =
0;
81             source.loop =
true;
82
83             
// start the clip from a random point
84             source.time = Random.Range(
0f, clip.length);
85             source.Play();
86             source.minDistance =
5;
87             source.maxDistance = maxRolloffDistance;
88             source.dopplerLevel =
0;
89             
return source;
90         }
91
92
93         
// unclamped versions of Lerp and Inverse Lerp, to allow value to exceed the from-to range
94         
private static float ULerp(float from, float to, float value)
95         {
96             
return (1.0f - value) * from + value * to;
97         }
98     }


Gõ tìm kiếm nhanh...